import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup } from '@angular/forms';
import { environment } from '@env/environment';
import { NzDrawerService } from 'ng-zorro-antd/drawer';
import { NzModalService } from 'ng-zorro-antd/modal';
import { NzNotificationService } from 'ng-zorro-antd/notification';
import { SystemDictChildrenComponent } from '../list-children/list-children.component';
import { SystemDictListEditComponent } from '../list-edit/list-edit.component';
import { SystemDictService } from '../service/service.service';

@Component({
  selector: 'app-list',
  templateUrl: './list.component.html',
  styleUrls: ['./list.component.less'],
})
export class SystemDictListComponent implements OnInit {
  searchForm: any = FormGroup;
  yesOrNo = environment.yesNo;
  isRun: number = -1;
  loading1 = false;
  dataList: any;
  pageInfo = {
    pi: 1,
    ps: 10,
    total: 0,
    loading: false,
  };
  switchValue = false;

  constructor(
    private fb: FormBuilder,
    private systemDictService: SystemDictService,
    private modalService: NzModalService,
    private notification: NzNotificationService,
    private drawerService: NzDrawerService,
  ) {}

  ngOnInit(): void {
    this.searchForm = this.fb.group({
      name: null,
      code: null,
      isRun: null,
    });
    this.load();
  }

  add(): void {
    this.modalService
      .create({
        nzContent: SystemDictListEditComponent,
        nzComponentParams: {},
        nzTitle: '添加父级字典项',
        nzFooter: null,
      })
      .afterClose.subscribe((res: any) => {
        if (res) {
          this.load();
        }
      });
  }

  children(record: any) {
    this.drawerService
      .create({
        nzContent: SystemDictChildrenComponent,
        nzContentParams: { record },
        nzBodyStyle: {
          height: 'calc(100% - 55px)',
          overflow: 'auto',
          'padding-bottom': '53px',
          'padding-top': '0',
        },
        nzTitle: '查看子项',
        nzWidth: 'calc(100vw - 200px)',
        nzHeight: '100%',
      })
      .afterClose.subscribe((res: any) => {
        this.load();
      });
  }

  edit(record: any): void {
    this.modalService
      .create({
        nzContent: SystemDictListEditComponent,
        nzComponentParams: { record },
        nzTitle: '编辑父级字典项',
        nzFooter: null,
      })
      .afterClose.subscribe((res: any) => {
        if (res) {
          this.load();
        }
      });
  }

  reset(): void {
    this.pageInfo.pi = 1;
    this.searchForm.reset();
    this.load();
  }

  search(): void {
    this.pageInfo.pi = 1;
    this.load();
  }
  //
  // remove(record: any) {
  //   this.gridAreaService.gridAreaManagementGroupDelete(record).subscribe(() => {
  //     this.load();
  //   });
  // }

  load(): void {
    const params = {
      ...this.searchForm.value,
      current: this.pageInfo.pi,
      size: this.pageInfo.ps,
    };
    //删除没有的属性和空的
    for (let key in params) {
      if (params.hasOwnProperty(key) && (params[key] == null || params[key] == undefined || params[key] === ' ')) {
        delete params[key];
      }
    }
    this.pageInfo.loading = true;
    this.systemDictService.page(params).subscribe((res) => {
      this.dataList = res.data.records;
      this.pageInfo.total = res.data.total;
      this.pageInfo.loading = false;
    });
  }

  remove(id: string, children: number): void {
    if (children != 0) {
      this.notification.create('error', '错误提示', '请先删除该字典下的所有子项');
      return;
    }
    this.systemDictService.remove(id).subscribe((res) => {
      this.load();
    });
  }

  clickSwitch(data: any): void {
    const change = {
      id: data['id'],
      isRun: -1,
    };
    if (data['isRun'] == 1) {
      change['isRun'] = 0;
    } else {
      change['isRun'] = 1;
    }
    // this.pageInfo.loading = true;
    this.systemDictService.changeIsRun(change).subscribe((res) => {
      this.load();
      // this.pageInfo.loading = false;
    });
  }
}
